package org.opsli.modulars.business.app.web;

import org.opsli.modulars.business.iot.protocol.ConvertCode;
import org.opsli.modulars.business.iot.protocol.StringToAscii;

import java.text.SimpleDateFormat;
import java.util.*;

public class Test {


    public static void main(String[] args) throws Exception{
       /* System.out.println(hexStringToBytes("30"));*/
     /*byte[] bytes=new byte[11];
        bytes[0]=0x00;
        bytes[1]=0x10;
        bytes[2]=0x00;
        bytes[3]=0x1E;
        bytes[4]=0x00;
        bytes[5]=0x02;
        bytes[6]=0x04;
        bytes[7]=0x00;
        bytes[8]=0x00;
        bytes[9]=0x05;
        bytes[10]=0x76;
        String s= ConvertCode.bytesToHexString(bytes);
        System.out.println(s);
        String ss=ConvertCode.getCRC3(bytes);
        System.out.println(ss);*/
       /*String s="0010001E00020400000576";
        String ss=ConvertCode.getCRC3(ConvertCode.hexString2Bytes(s));
        System.out.println(ss);*/
      /* String s="00000786";
       System.out.println(change(Integer.valueOf(s)));*/
        /*String s="0105"+"0036"+"0002";*/
      /*  String s="000300460008";
        String ss=ConvertCode.getCRC3(ConvertCode.hexString2Bytes(s));
        System.out.println(s+ss);*/
      /*String s="00035e0020220470003133392e3030392e3032362e303930000000000000000000000000000000075b00000000000000000000000000000000000000000001000a00003130312e3230302e3035352e3132310000000000000000000000000000002f22";
        //mqttPORT
        String s9=s.substring(190,194);
        Integer mqttport=covert(s9);
        System.out.println(mqttport);*/
       /* String h1="0010"+"001F"+"0005"+"0A";
        String h2="0010"+"0024"+"0005"+"0A";
        String h3="0010"+"0029"+"0005"+"0A";
        String s2= StringToAscii.parseAscii("139.009.026.090");
        if(s2.length()<60){
            Integer diff=60-s2.length();
            for(int i=0;i<diff;i++){
                s2=s2+"0";
            }
        }
        String p1=s2.substring(0,20);
        String p2=s2.substring(20,40);
        String p3=s2.substring(40,60);
        String hex1=h1+p1;
        String hex2=h2+p2;
        String hex3=h3+p3;
        hex1=hex1+ConvertCode.getCRC3(ConvertCode.hexString2Bytes(hex1));
        hex2=hex2+ConvertCode.getCRC3(ConvertCode.hexString2Bytes(hex2));
        hex3=hex3+ConvertCode.getCRC3(ConvertCode.hexString2Bytes(hex3));
        System.out.println(hex1);
        System.out.println(hex2);
        System.out.println(hex3);
        System.out.println(hex1.length());*/

        /*String h1="0010"+"0003"+"0005"+"0A";
        String h2="0010"+"0008"+"0005"+"0A";
        String h3="0010"+"000D"+"0005"+"0A";
        String s2= StringToAscii.parseAscii("117.158.204.100");
        if(s2.length()<60){
            Integer diff=60-s2.length();
            for(int i=0;i<diff;i++){
                s2=s2+"0";
            }
        }
        String p1=s2.substring(0,20);
        String p2=s2.substring(20,40);
        String p3=s2.substring(40,60);
        String hex1=h1+p1;
        String hex2=h2+p2;
        String hex3=h3+p3;
        hex1=hex1+ConvertCode.getCRC3(ConvertCode.hexString2Bytes(hex1));
        hex2=hex2+ConvertCode.getCRC3(ConvertCode.hexString2Bytes(hex2));
        hex3=hex3+ConvertCode.getCRC3(ConvertCode.hexString2Bytes(hex3));
        System.out.println(hex1);
        System.out.println(hex2);
        System.out.println(hex3);
        System.out.println(hex1.length());*/

        /*String sjStr="002022010033,\n" +
                "002022010037,\n" +
                "002022010036,\n" +
                "002022010022,\n" +
                "002022010039,\n" +
                "002022010031,\n" +
                "002022010025,\n" +
                "002022010024,\n" +
                "002022010034,\n" +
                "002022010023,\n" +
                "002022010032,\n" +
                "002022010027,\n" +
                "002022010030,\n" +
                "002022010035,\n" +
                "002022010029,\n" +
                "002022010038,\n" +
                "002022010010,\n" +
                "002022010011,\n" +
                "002022010012,\n" +
                "002022010014,\n" +
                "002022010015,\n" +
                "002022010017,\n" +
                "002022010018,\n" +
                "002022010019,\n" +
                "002022010020,\n" +
                "002022010021,\n" +
                "002022010042,\n" +
                "002022010045,\n" +
                "002022010047,\n" +
                "002022010048,\n" +
                "002022010050";
        String key="0020220100463";
        if(sjStr.contains(key)){
            System.out.println("a");
        }else{
            System.out.println("b");
        }*/
        /*System.out.println(getFormatDate());*/
        System.out.println(ZkUtils.getRandomString(32));


    }

    private static String getFormatDate() throws Exception{
        String updated="";
        GregorianCalendar ca = new GregorianCalendar();
        int hour = ca.get(Calendar.HOUR_OF_DAY);
        if(hour<8){
            updated="昨天 08:00";
        }else{
            updated="今天 08:00";
        }
                if (updated.substring(0, 2).equals("今天")){
                        String partOfDate = updated.substring(3);
                         Calendar calendar = Calendar.getInstance();
                         calendar.setTime(new Date());
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                         SimpleDateFormat todaySdf = new SimpleDateFormat("yyyy-MM-dd");
                         String today = todaySdf.format(calendar.getTime());
                        String finalString = today + " " + partOfDate;
                        /* SimpleDateFormat finalSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        return finalSdf.parse(finalString);*/
                        return finalString;
                     }else if (updated.substring(0,2).equals("昨天")){
                         String partOfDate = updated.substring(3);
                         Calendar calendar = Calendar.getInstance();
                        calendar.setTime(new Date());
                         calendar.add(Calendar.DATE, -1);
                         calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                         calendar.set(Calendar.SECOND, 0);
                         SimpleDateFormat yesterdaySdf = new SimpleDateFormat("yyyy-MM-dd");
                         String yesterday = yesterdaySdf.format(calendar.getTime());
                         String finalString = yesterday + " " + partOfDate;
                        /* SimpleDateFormat finalSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                         return finalSdf.parse(finalString);*/
                        return finalString;
                    }else {
                        /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");*/
                         return ZkUtils.getDateStr();
                     }
            }

    public static String BCDToStr(byte bcd) {

        int a = (bcd >> 4);
        int b = bcd & 0x0f;
        String s=String.valueOf(a) + String.valueOf(b);
        return s.replace("-","");
    }

    public static byte hexStringToBytes(String hexString) {

        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d[0];
    }

    public static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static String change(int n) {
        if (n < 0)

            return "-" + change(-n);

        if (n < 16) {
            return "0123456789ABCDEF".charAt(n) + "";

        } else {
            return change(n / 16) + change(n % 16);

        }
    }

    /**
     * @param: [content]
     * @return: int
     * @description: 十六进制转十进制
     */
    public static int covert(String content){
        int number=0;
        String [] HighLetter = {"A","B","C","D","E","F"};
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0;i <= 9;i++){
            map.put(i+"",i);
        }
        for(int j= 10;j<HighLetter.length+10;j++){
            map.put(HighLetter[j-10],j);
        }
        String[]str = new String[content.length()];
        for(int i = 0; i < str.length; i++){
            str[i] = content.substring(i,i+1);
        }
        for(int i = 0; i < str.length; i++){
            number += map.get(str[i])*Math.pow(16,str.length-1-i);
        }
        return number;
    }

}
